package com.sunlgggg.view.component;

import com.google.gson.Gson;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.util.HashMap;

import static com.sunlgggg.util.HttpRequestUtils.*;

/**
 * @author sunlggggg
 * @date 2016/12/1
 */

public class MenuBar extends JMenuBar {
    public MenuBar() {
        //文件菜单
        JMenu fileMenu = new JMenu("文件");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        JMenuItem exitItem = new JMenuItem("退出系统");
        exitItem.addActionListener(e -> System.exit(1));
        fileMenu.add(exitItem);


        //统计菜单
        JMenu censusJMenu = new JMenu("统计");

        //子菜单
        JMenu everyCourseScoreJMenu = new JMenu("各科成绩");

        JMenuItem everyCourseAverageItem = new JMenuItem("各门课程平均分");

        everyCourseAverageItem.addActionListener(e -> {
            String message = get("info/avg");
            Gson gson = new Gson();
            HashMap hashMap = gson.fromJson(message, HashMap.class);
            String english = String.valueOf(hashMap.get("english"));
            String math = String.valueOf(hashMap.get("math"));
            String chinese = String.valueOf(hashMap.get("chinese"));

            JOptionPane.showConfirmDialog(null, "语文:" + chinese + "\n" + "数学:" + math + "\n" + "英语:" + english + "\n", "", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
        });
        JMenuItem everyCourseHighestItem = new JMenuItem("各门课程最高分");
        everyCourseHighestItem.addActionListener(e -> {
            String message = get("info/max");
            Gson gson = new Gson();
            HashMap hashMap = gson.fromJson(message, HashMap.class);
            String english = String.valueOf(hashMap.get("english"));
            String math = String.valueOf(hashMap.get("math"));
            String chinese = String.valueOf(hashMap.get("chinese"));

            JOptionPane.showConfirmDialog(this, "语文:" + chinese + "\n" + "数学:" + math + "\n" + "英语:" + english + "\n", "", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
        });
        JMenuItem everyCourseLowestItem = new JMenuItem("各门课程最低分");

        everyCourseLowestItem.addActionListener(e -> {
            String message = get("info/min");
            Gson gson = new Gson();
            HashMap hashMap = gson.fromJson(message, HashMap.class);
            String english = String.valueOf(hashMap.get("english"));
            String math = String.valueOf(hashMap.get("math"));
            String chinese = String.valueOf(hashMap.get("chinese"));
            JOptionPane.showConfirmDialog(this, "语文:" + chinese + "\n" + "数学:" + math + "\n" + "英语:" + english + "\n", "", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
        });

        everyCourseScoreJMenu.add(everyCourseAverageItem);
        everyCourseScoreJMenu.add(everyCourseHighestItem);
        everyCourseScoreJMenu.add(everyCourseLowestItem);

        JMenuItem allStudentNumberItem = new JMenuItem("所有学生人数");

        allStudentNumberItem.addActionListener(e -> {
            String message = get("info/count");
            JOptionPane.showConfirmDialog(null, "所有学生人数:" + message, "", JOptionPane.CLOSED_OPTION, JOptionPane.INFORMATION_MESSAGE);
        });


        censusJMenu.add(everyCourseScoreJMenu);
        censusJMenu.add(allStudentNumberItem);

        //系统外观菜单
        //子菜单
        JMenu systemAppearenceJMenu = new JMenu("系统外观");

        JRadioButtonMenuItem systemAppearencejRadioButtonMenuItem = new JRadioButtonMenuItem();
        JRadioButton metalStyleJRadioButton = new JRadioButton("Metal风格");
        JRadioButton motifStyleJRadioButton = new JRadioButton("Motif");
        JRadioButton windowsStyleJRadioButton = new JRadioButton("Windows风格");
        metalStyleJRadioButton.setSelected(true);
        metalStyleJRadioButton.addActionListener((ActionEvent e) -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  //设置界面样式
                UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                UIManager.getCrossPlatformLookAndFeelClassName();
                javax.swing.SwingUtilities.updateComponentTreeUI(this);
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }
            metalStyleJRadioButton.setSelected(true);
            motifStyleJRadioButton.setSelected(false);
            windowsStyleJRadioButton.setSelected(false);
        });
        motifStyleJRadioButton.addActionListener(e -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  //设置界面样式
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                UIManager.getCrossPlatformLookAndFeelClassName();
                javax.swing.SwingUtilities.updateComponentTreeUI(this);
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }
            metalStyleJRadioButton.setSelected(false);
            motifStyleJRadioButton.setSelected(true);
            windowsStyleJRadioButton.setSelected(false);
        });
        windowsStyleJRadioButton.addActionListener(e -> {
            try {
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());  //设置界面样式
                UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                UIManager.getCrossPlatformLookAndFeelClassName();
                javax.swing.SwingUtilities.updateComponentTreeUI(this);
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e1) {
                e1.printStackTrace();
            }
            metalStyleJRadioButton.setSelected(false);
            motifStyleJRadioButton.setSelected(false);
            windowsStyleJRadioButton.setSelected(true);
        });

        systemAppearencejRadioButtonMenuItem.add(metalStyleJRadioButton);
        systemAppearenceJMenu.add(metalStyleJRadioButton);
        systemAppearenceJMenu.add(motifStyleJRadioButton);
        systemAppearenceJMenu.add(windowsStyleJRadioButton);


        add(fileMenu);
        add(censusJMenu);
        add(systemAppearenceJMenu);
    }
}
